package com.demo.core.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.core.dao.OrgDao;
import com.demo.core.dao.impl.base.PageCoreDaoSupport;
import com.demo.core.model.Org;
import com.demo.core.model.support.GeneralExecuteModel;
import com.demo.core.model.support.converter.AdjacencyListToChainedObjectConverter;

/**
 * 机构数据访问层实现类
 * 
 * @author ryuu.kk
 */
public class OrgDaoImpl extends PageCoreDaoSupport<Org, Long> implements OrgDao {
	
	/**
	 * 机构空间 
	 */
	protected final static String ORG_SPACE = "orgSpace.";
	
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CORE_ORG";
	
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";

	@Override
	public int updateInfo(Org org, Map<String, Object> orgInfo) {
		return super.update(org, orgInfo);
	}

	@Override
	public int updateStatus(Org org, Map<String, Object> params) {
		cacheClear();
		params.put("org", org);
		return getSqlMapClientTemplate().update(ORG_SPACE + "changeOrgStatus", params);
	}

	@Override
	public int updateArea(Org org, Map<String, Object> params) {
		cacheClear();
		params.put("org", org);
		return getSqlMapClientTemplate().update(ORG_SPACE + "changeOrgArea", params);
	}
	@Override
	public int delete(Long id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		delete(params);
		return Integer.parseInt(params.get("cnt").toString());
	}
	@Override
	public int updateParent(Map<String, Object> orgParent) {
		cacheClear();
		return getSqlMapClientTemplate().update(ORG_SPACE + "changeOrgParent", orgParent);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Org tree(Map<String, Object> params) {
		// 根节点
		Org root = null;
		List<Org> orgList = getSqlMapClientTemplate().queryForList(nameSpace() + "queryTree", params);
		root = AdjacencyListToChainedObjectConverter.conver(orgList);
		return root;
	}
	
	@Override
	protected String nameSpace() {
		return ORG_SPACE;
	}
	
	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
	}
}
